package com.itheima.controller;

import com.itheima.common.entity.PageResult;
import com.itheima.common.entity.QueryPageBean;
import com.itheima.common.entity.Result;
import com.itheima.pojo.dto.SetmealDTO;
import com.itheima.pojo.entity.Setmeal;
import com.itheima.service.SetmealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;

@RestController
@RequestMapping("/setmeal")
public class SetmealController {

    @Autowired
    private SetmealService setmealService;


    @PostMapping("/add")
    public Result addSetmeal(@RequestBody SetmealDTO setmealDTO){
        setmealService.addSetmeal(setmealDTO);
        return new Result(true,"新增套餐成功");
    }

    @PostMapping("/edit")
    public Result editSetmeal(@RequestBody Setmeal setmeal){
        setmealService.editSetmeal(setmeal);
        return new Result(true,"编辑套餐成功");
    }

    @GetMapping("/findById")
    public Result findByIdSetmeal(String id){
        Setmeal setmeal=setmealService.findByIdSetmeal(id);
        return new Result(true,"查询套餐成功",setmeal);
    }

    @PostMapping("/findPage")
    public PageResult findPageSetmeal(@RequestBody QueryPageBean queryPageBean){
        PageResult pageResult=setmealService.findPageSetmeal(queryPageBean);
        return  pageResult;
    }


    @PostMapping("/upload")
    public Result uploadSetmeal(MultipartFile imgFile) throws IOException {
        String url = setmealService.uploadSetmeal(imgFile);
        return new Result(true, "上传成功", url);
    }

    @PostMapping("/getSetmeal")
    public Result getSetmeal(){
        List<Setmeal> list= setmealService.getSetmeal();
        return new Result(true,"查询套餐数据成功",list);
    }

    @PostMapping("/findById")
    public Result findById(String id){
        Setmeal setmeal=setmealService.findById(id);
        return new Result(true,"查询套餐数据成功",setmeal);
    }
}
